<template>
  <div class="login">
    <div class="lift">
      <div class="login-title">
        <img class="t1" src="@/assets/img/01.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form :model="form" class="form" :rules="rules" ref="from">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" prefix-icon="el-icon-key" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="8">
              <img @click="codeclick" class="login-code" :src="baseUrl+codeurl" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="ispass">
          <el-checkbox v-model="form.ispass" class="ispass" :label="true">
            我已经阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="tologin">登陆</el-button>
          <br />
          <el-button type="primary" class="btn" @click="toregister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img class="right" src="@/assets/img/login.png" alt />
    <register ref="register"></register>
  </div>
</template>

<script>
import register from "./register";
import { savelocal,getlocal } from "@/utils/local.js";
import { userlogin } from "@/api/login.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      codeurl: "/captcha?type=login",
      baseUrl: process.env.VUE_APP_URL,
      input2: "",
      input: "",
      form: {
        phone: "",
        password: "",
        code: "",
        ispass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 11, message: "请输入6-12位密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" },
        ],
        ispass: [{ required: true, message: "请勾选协议", trigger: "change" }],
      },
    };
  },
  created(){
    if(getlocal('token')){
      this.$router.push('/layout')
    }
  },
  methods: {
    codeclick() {
      this.codeurl = "/captcha?type=login&jfidfji=" + Date.now();
    },
    tologin() {
      this.$refs.from.validate((result) => {
        if (result) {
          // this.$message.success("验证成功")
          userlogin(this.form).then((res) => {
            console.log(res);
            this.$message.success("登录成功");
            // localStorage.setItem('token',res.data.data.token)
            savelocal("token", res.data.data.token);
            this.$router.push('/layout')
          });
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    toregister() {
      this.$refs.register.isshow = true;
    },
  },
};
</script>

<style lang="less">
.login {
  width: 1400px;
  height: 788px;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .lift {
    width: 478px;
    height: 550px;
    padding: 42px;
    background-color: #f5f5f5;
    .login-title {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
        height: 18px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 14px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background-color: #c7c7c7;
        margin-right: 12px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 30px;
      .login-code {
        width: 100%;
        height: 40px;
      }
      .btn {
        display: block;
        width: 100%;
      }
      .btn:nth-child(2) {
        margin-top: 25px;
      }
    }
  }
}
</style>